﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using WareHose.Application.DePartment.DTo;
using WareHose.Application.Menus.Dto;
using WareHouse.Domain;
using WareHouse.Repositories;

namespace WareHose.Application.DePartment
{
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v2")]

    public class DePartmentSerevice : IDynamicWebApi, IDePartmentSerevice
    {

        private readonly ISqlSugarClient _sqlSugarClient;

        private readonly IBaseRepository<Department> _departmentRepository;

        private readonly IMapper mapper;
        public DePartmentSerevice(IBaseRepository<Department> departmentRepository, ISqlSugarClient _sqlSugarClient,
            IMapper mapper
            )
        {
            this.mapper = mapper;
            this._departmentRepository = departmentRepository;
            this._sqlSugarClient = _sqlSugarClient;
        }

        /// <summary>
        /// 部门显示
        /// </summary>
        /// <param name="DName"></param>
        /// <param name="Remark"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultDto<List<Department>>> MentSHow(string? DName, string? Remark)
        {

            var tree = _sqlSugarClient.Queryable<Department>().WhereIF(!string.IsNullOrEmpty(DName),x=>x.DName.Contains(DName))
                .WhereIF(!string.IsNullOrEmpty(Remark),x=>x.Remark.Contains(Remark))
                
                .ToTree(it => it.Children, it => it.Pid, 0);


            return new ResultDto<List<Department>>
            {
                Code = Status.Success,
                Data = tree,
                Message = "成功"

            };

        }

        /// <summary>
        /// 部门添加
        /// </summary>
        /// <param name="Department"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultDto> MentAdd(MentDto mentDto)
        {


             var list =  mapper.Map<MentDto,Department>(mentDto);

               await _departmentRepository.InsertAsync(list);


              return new ResultDto { Code = Status.Success, Message = "添加成功" };
        }

        /// <summary>
        /// 部门删除
        /// </summary>
        /// <param name="Did"></param>
        /// <returns></returns>
        [HttpDelete]
        public  async Task<ResultDto> DeleteAsync(string Did)
        {
            try
            {
              
                if(Did != null)
                {
                    string[] ids = Did.Split(',');
                    foreach (var item in ids)
                    {
                        await _departmentRepository.DeleAsync(Convert.ToInt64(item));
                    }
                    return new ResultDto { Code = Status.Success, Message = "删除成功" };

                }
                else
                {
                    return new ResultDto { Code = Status.Success, Message = "删除失败" };
                }
                               
            }
            catch (Exception e)
            {
                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }


        /// <summary>
        /// 部门修改
        /// </summary>
        /// <param name="mentDto"></param>
        /// <returns></returns>

        [HttpPut]
        public async Task<ResultDto> UPdateAsync(MentDto mentDto)
        {
            try
            {
                var list = mapper.Map<MentDto, Department>(mentDto);

                await _departmentRepository.UpdateAsync(list);

                return new ResultDto { Code = Status.Success, Message = "修改成功" };
            }
            catch (Exception e)
            {

               return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
            




        }
    }
}
